package com.example.alertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1, btn2, btn3, btn4, btn5;
    private int checked3 = 0;  // myDialog3

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置视图
        setContentView(R.layout.activity_main);

        // 获取id
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);

        // 设置点击时间，要传入一个实现了OnClickListener接口的类
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                AlertDialog.Builder myDialog1 = new AlertDialog.Builder(MainActivity.this);
                myDialog1.setIcon(R.drawable.disk_s);
                myDialog1.setTitle("标题");
                myDialog1.setMessage("msg");
                myDialog1.setCancelable(false);//点击对话框外的灰色部分对话框不会消失
                myDialog1.setNeutralButton("左边按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {  // which=3
                        Toast.makeText(MainActivity.this, "左边按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog1.setNegativeButton("中间按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {  // which=2
                        Toast.makeText(MainActivity.this, "中间按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog1.setPositiveButton("右边按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {  // which=1
                        Toast.makeText(MainActivity.this, "右边按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog1.show();
                break;

            case R.id.btn2:
                String[] string2 = new String[]{"a", "b", "c", "d","a", "b", "c", "d","a", "b", "c", "d","a", "b", "c", "d","a", "b", "c", "d"};
                AlertDialog.Builder myDialog2 = new AlertDialog.Builder(MainActivity.this);
                myDialog2.setTitle("标题").setItems(string2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, string2[which], Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;

            case R.id.btn3:
                String[] string3 = new String[]{"a", "b", "c", "d"};
                AlertDialog.Builder myDialog3 = new AlertDialog.Builder(MainActivity.this);
                myDialog3.setTitle("标题").setSingleChoiceItems(string3, 0, new DialogInterface.OnClickListener() {
                    @Override  // 第二个参数是默认被选中的
                    public void onClick(DialogInterface dialog, int which) {
                        checked3 = which;
                        Toast.makeText(MainActivity.this, string3[which], Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你选了" + string3[checked3], Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;

            case R.id.btn4:
                String[] string4 = new String[]{"a", "b", "c", "d"};
                boolean[] checked4 = {true, false, false, false};  // 数组里的值会跟着点击自己变
                new AlertDialog.Builder(MainActivity.this).setMultiChoiceItems(string4, checked4, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override//第二个参数是是否被默认选中
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(MainActivity.this, string4[which] + isChecked, Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, Arrays.toString(checked4), Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;

            case R.id.btn5:
                AlertDialog.Builder myDialog5 = new AlertDialog.Builder(MainActivity.this);
                // 根据xml加载view
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog5, null);
                // 从view里获取按钮
                Button btn_login = view.findViewById(R.id.dialog_btn);
                btn_login.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "你点击了登录按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                // 设置视图
                myDialog5.setTitle("登录界面").setView(view).show();
                break;
        }
    }
}